<?php
function myLOGTest ($str)
{
    $add_str = $str . "\n";
    if (!( $fp = fopen("myLOGTest.txt", "a") ))
    {
        echo "open error";
    }
    else
    {
        fwrite($fp, $add_str);
        fpassthru($fp);
    }
}
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
if ($chkGametype != 3)
{
    exit("error gametype");
}
$chk = new CheckMode_3d($curruser['acl'], 5, $gametype);
$chk->checkShow3dPage();
$betname_3d = judge_type($gametype);
unset($chk);
if ($curruser['prev_id'] != 0)
{
    exit();
}
$tbl = $g_gametype_3d[$gametype]['en'];
if (20 < $gametype)
{
    $_8hFlag = true;
}
else
{
    $_8hFlag = false;
}
require_once( "class_5h/Bet8_5h.php" );
require_once( "class_5h/RateConfig_5h.php" );
require_once( "class_5h/class.lotto5h.php" );
$conf = new Config();
$Bet8_3d = new Bet8_5h($gametype);
$RateConfig_5h = new RateConfig_5h($gametype);
$lotto5h = new lotto5h($gametype);
$playtype = getvar("playtype");
$lotto_id = intval(getvar("lotto_id_3d"));
$insertFlag = false;
$xyMode = intval($conf->get("5hxyMode"));
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    exit();
}
else if ($playtype == "" || !isset($RateConfig_5h->playGroupArr02[$playtype]))
{
    exit();
}
else if ($lotto_id < 0)
{
    exit();
}
if ($g_conf['language'] == "zh-cn")
{
    $headerLan = "; charset=gb2312";
    $chgLan = true;
    include_once( "class/ccharset.php" );
    $cc = new CCharset();
}
else
{
    $headerLan = "; charset=utf-8";
    $chgLan = false;
}
$note = "";
$BtnRate = floatval($_POST['BtnRate']);
$BtnBack = floatval($_POST['BtnBack']);
$BtnAmount = intval($_POST['BtnAmount']);
$BtnNote = htmlspecialchars($_POST['BtnNote'], ENT_QUOTES);
$showSubid = intval(getvar("subid"));
$showSubid = isset($RateConfig_5h->subArr[$showSubid]) ? $showSubid : "0";
$showAllSubArr = array("rnp");
if ($BtnRate < 1)
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain{$headerLan}");
    $msg = "001-赔率值错误";
    if ($chgLan)
    {
        $msg = $cc->Gb_Big5($msg);
    }
    echo "{\"msg\":\"" . $msg . "\"}";
    exit();
}
else if ($BtnAmount < 0)
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain{$headerLan}");
    $msg = "002-金额错误";
    if ($chgLan)
    {
        $msg = $cc->Gb_Big5($msg);
    }
    echo "{\"msg\":\"" . $msg . "\"}";
    exit();
}
$fields = array();
$fields['lotto_id'] = $lotto_id;
$fields['createtime'] = systime();
$fields['amount'] = $BtnAmount;
$fields['rate'] = $BtnRate;
$fields['back'] = $BtnBack;
$fields['state'] = "0";
$fields['account'] = "'{$BtnNote}'";
if ($_8hFlag)
{
    $rcNumArr = array(10 => 2, 11 => 2, 12 => 2, 13 => 3, 14 => 3, 15 => 3, 16 => 4, 17 => 5);
    $subEndNum = 9;
    $rcNoSortArr = array(12, 15);
}
else
{
    $rcNumArr = array(9 => 1, 10 => 2, 11 => 3, 12 => 3, 13 => 4, 14 => 5);
    $subEndNum = 6;
    $rcNoSortArr = array();
}
foreach ($RateConfig_5h->playGroupArr02[$playtype] as $val)
{
    if (in_array($val, $g_betXYArr[$gametype]))
    {
        if ($xyMode)
        {
            $xyid = intval(getvar("xyid"));
            if (!$xyid)
            {
                exit("error xyid");
            }
            if (!isset($RateConfig_5h->xyidTitle[$xyid]))
            {
                exit("error xyid-2");
            }
        }
        else
        {
            $xyid = 1;
        }
    }
    else
    {
        $xyid = 0;
    }
    $fields['xyid'] = $xyid;
    if (in_array($val, $g_bet4subArr[$gametype]))
    {
        if (in_array($playtype, $showAllSubArr))
        {
            if ($showSubid < 1)
            {
                header("Cache-Control: no-cache, must-revalidate");
                header("Content-Type: text/plain{$headerLan}");
                $msg = "003-玩法错误";
                if ($chgLan)
                {
                    $msg = $cc->Gb_Big5($msg);
                }
                echo "{\"msg\":\"" . $msg . "\"}";
                exit();
            }
            $subStart = $showSubid;
            $subEnd = $showSubid + 1;
        }
        else
        {
            $subStart = 0;
            $subEnd = $subEndNum;
        }
    }
    else
    {
        $subStart = 0;
        $subEnd = 1;
    }
    $subid = $subStart;
    while ($subid < $subEnd)
    {
        $postidx = "unow{$val}_{$subid}";
        $betSubIdx = "{$val}_{$subid}";
        if (isset($_POST[$postidx]) && is_array($_POST[$postidx]))
        {
            $postarr = $_POST[$postidx];
            $fields['bettype'] = $val;
            $fields['subid'] = $subid;
            if (isset($rcNumArr[$val]))
            {
                $chkNumArr = explode(",", $postarr[0]);
                if (!in_array($val, $rcNoSortArr))
                {
                    sort($chkNumArr);
                }
                if (count($chkNumArr) != $rcNumArr[$val])
                {
                    $msg = "004-玩法" . $betname_3d[$betSubIdx] . "不正确的勾选个数(连码),走飞失败！停止走飞。请手动刷新资料。";
                    if ($chgLan)
                    {
                        $msg = $cc->Gb_Big5($msg);
                    }
                    $note .= $msg;
                    echo "{\"msg\":\"" . $note . "\"}";
                    exit();
                }
                else
                {
                    $numStrTemp = implode(",", $chkNumArr);
                    $fields['numbers'] = "'" . $numStrTemp . "'";
                    $fields['line_nums'] = "'" . $RateConfig_5h->getBetContent($val, $numStrTemp) . "'";
                    $insertFlag = $Bet8_3d->dbInsert($fields);
                    if ($insertFlag != false)
                    {
                        $lotto5h->getBet8_5H($insertFlag);
                        $insertFlag = true;
                        $msg = "玩法{$betname_3d[$betSubIdx]}走飞完成！";
                        if ($chgLan)
                        {
                            $msg = $cc->Gb_Big5($msg);
                        }
                        $note .= $msg;
                    }
                }
            }
            else
            {
                foreach ($postarr as $val2)
                {
                    $fields['numbers'] = "'" . $val2 . "'";
                    $fields['line_nums'] = "'" . $RateConfig_5h->getBetContent($val, $val2) . "'";
                    $insertFlag = $Bet8_3d->dbInsert($fields);
                    if ($insertFlag != false)
                    {
                        $lotto5h->getBet8_5H($insertFlag);
                        $insertFlag = true;
                        $msg = "玩法{$betname_3d[$betSubIdx]}走飞完成！";
                        if ($chgLan)
                        {
                            $msg = $cc->Gb_Big5($msg);
                        }
                        $note .= $msg;
                    }
                }
            }
        }
        ++$subid;
    }
}
if ($insertFlag == false)
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"error\"}";
}
else
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"OK\"}";
}
exit();
?>